<?php
class ModelDesignMenu extends Model {
	public function addMenu($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "menu SET name = '" . $this->db->escape($data['name']) . "',code = '" . $this->db->escape($data['code']) . "'");
	
		$menu_id = $this->db->getLastId();
		$parent_menu_id=0;
		if (isset($data['menu_route'])) {
			foreach ($data['menu_route'] as $menu_route) {
				$parent_menu_id=$this->menufilled($menu_route,$menu_id);
				if(isset($menu_route['sub'])){
					foreach ($menu_route['sub'] as $menu_route_sub) {
							$this->menufilled($menu_route_sub,$menu_id,$parent_menu_id);
					}
					continue;
				}
			}
		}	
	}
	
	private function menufilled($menu_route_sub,$menu_id,$parent_menu_id=0) {
	//	if(isset($menu_route_sub['row'])){
			if($menu_route_sub['type']=='cus'){
				$this->db->query("INSERT INTO " . DB_PREFIX . "menu_route SET menu_id = '" . (int)$menu_id . "',  parent_menu_id	 = '" . (int)$parent_menu_id . "', menu_type = '" . $menu_route_sub['type'] . "', sort_order = '" . (int)$menu_route_sub['sort_order'] . "',   route = '" . $this->db->escape($menu_route_sub['route_url']) . "'");
				$parent_menu_id=$this->db->getLastId();
				$sub_cus_menu_id=$this->db->getLastId();
				foreach ($menu_route_sub as $language_id => $value) {
					if($language_id!=0)
							$this->db->query("INSERT INTO " . DB_PREFIX . "menu_cus SET menu_route_id = '" . (int)$sub_cus_menu_id . "',language_id = '" . (int)$language_id . "', display_name = '" . $value['route_name'] . "'");
				}
			}else if($menu_route_sub['type']=='fixed'){ 
					$arr = explode("-",$menu_route_sub['text']);
					$this->db->query("INSERT INTO " . DB_PREFIX . "menu_route SET menu_id = '" . (int)$menu_id . "', parent_menu_id	 = '" . (int)$parent_menu_id . "',  menu_type = '" . $menu_route_sub['type'] . "', sort_order = '" . (int)$menu_route_sub['sort_order'] . "', display_name = '" . $this->db->escape($arr[1]) . "', route = '" . $this->db->escape($arr[0]) . "'");
					$parent_menu_id=$this->db->getLastId();
			}else{
					 $this->db->query("INSERT INTO " . DB_PREFIX . "menu_route SET parent_menu_id	 = '" . (int)$parent_menu_id . "', menu_id = '" . (int)$menu_id . "',  menu_type = '" . $menu_route_sub['type'] . "', sort_order = '" . (int)$menu_route_sub['sort_order'] . "', route = '" . $this->db->escape($menu_route_sub['text']) . "'");
					 $parent_menu_id=$this->db->getLastId();
			}
	//	}
		return $parent_menu_id;
	}
	
	public function editMenu($menu_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "menu SET name = '" . $this->db->escape($data['name']) . "',code = '" . $this->db->escape($data['code']) . "' WHERE Menu_id = '" . (int)$menu_id . "'");
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "menu_cus WHERE menu_route_id IN ( SELECT menu_route_id FROM  " . DB_PREFIX . "menu_route  WHERE menu_id = '" . (int)$menu_id . "')");
		$this->db->query("DELETE FROM " . DB_PREFIX . "menu_route WHERE menu_id = '" . (int)$menu_id . "'");
		
		$parent_menu_id=0;
		if (isset($data['menu_route'])) {
			foreach ($data['menu_route'] as $menu_route) {
				$parent_menu_id=$this->menufilled($menu_route,$menu_id);
	
				if(isset($menu_route['sub'])){
					
					foreach ($menu_route['sub'] as $menu_route_sub) {
							$this->menufilled($menu_route_sub,$menu_id,$parent_menu_id);
					}
					continue;
				}
			}
		}	
	}
	
	public function deleteMenu($menu_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "menu WHERE menu_id = '" . (int)$menu_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "menu_cus WHERE menu_route_id IN ( SELECT menu_route_id FROM  " . DB_PREFIX . "menu_route  WHERE menu_id = '" . (int)$menu_id . "')");
		$this->db->query("DELETE FROM " . DB_PREFIX . "menu_route WHERE menu_id = '" . (int)$menu_id . "'");
	
	}
	
	public function getMenu($menu_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "menu WHERE menu_id = '" . (int)$menu_id . "'");
		
		return $query->row;
	}
		 
	public function getMenus($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "menu";
		
		$sort_data = array('name');	
		
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY name";	
		}
		
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}					

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
		
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}		
		
		$query = $this->db->query($sql);

		return $query->rows;
	}
	
	public function getMenuRoutes($menu_id) {
		$menu_routes =array();
		$sql="SELECT * FROM " . DB_PREFIX . "menu_route WHERE menu_id = '" . (int)$menu_id . "' AND parent_menu_id=0 ORDER BY sort_order";
		$query = $this->db->query($sql);
		
		if($query->rows){
			$menus=$query->rows;
			foreach ($menus as $menu) {
				$sub_sql="SELECT * FROM " . DB_PREFIX . "menu_route WHERE menu_id = '" . (int)$menu_id . "' AND parent_menu_id=".$menu['menu_route_id']." ORDER BY sort_order";
				
				$sub_query = $this->db->query($sub_sql);
				$sub_menu_routes =array();
				
			if($sub_query->rows){
					$sub_menus=$sub_query->rows;
					foreach ($sub_menus as $sub_menu) {
					if($sub_menu['menu_type']=='cus'){
						$sub_cus_sql="SELECT * FROM " . DB_PREFIX . "menu_cus WHERE menu_route_id=".$sub_menu['menu_route_id'];
						$sub_cus_query = $this->db->query($sub_cus_sql);
						if($sub_cus_query->rows){
							$sub_menu_routes[]=array(
								'menu_route_id'=> $sub_menu['menu_route_id'],
								'type'=> $sub_menu['menu_type'],
								'sort_order'=> $sub_menu['sort_order'],
								'display_name'=>'',
								'route'=> $sub_menu['route'],
								'route_name'=>$sub_cus_query->rows
							);
						}
					}else{
							$sub_menu_routes[]=array(
								'menu_route_id'=> $sub_menu['menu_route_id'],
								'type'=> $sub_menu['menu_type'],
								'sort_order'=> $sub_menu['sort_order'],
								'route'=> $sub_menu['route'],
								'display_name'=> $sub_menu['display_name']
						);
					 }
					}
				}
				
				if($menu['menu_type']=='cus'){
					$cus_sql="SELECT * FROM " . DB_PREFIX . "menu_cus WHERE menu_route_id=".$menu['menu_route_id'];
					$cus_query = $this->db->query($cus_sql);
					if($cus_query->rows){
						$menu_routes[] = array(
						    'menu_route_id'=> $menu['menu_route_id'],
							'type'=> $menu['menu_type'],
							'sort_order'=> $menu['sort_order'],
							'display_name'=>'',
							'route'=> $menu['route'],
							'route_name'=> $cus_query->rows,
							'sub'=> $sub_menu_routes
							
						);
					}
				}else{
					$menu_routes[]=array(
							'menu_route_id'=> $menu['menu_route_id'],
							'type'=> $menu['menu_type'],
							'sort_order'=> $menu['sort_order'],
							'route'=> $menu['route'],
							'display_name'=> $menu['display_name'],
							'sub'=> $sub_menu_routes
					);
				}
			}
		}
		return $menu_routes;
	}
		
	public function getRoute($menu_id) {
		$sql="SELECT * FROM " . DB_PREFIX . "menu_route WHERE menu_route_id = '" . (int)$menu_id . "'";
		$query = $this->db->query($sql);
		return $query->row;
	}
	
	public function getCusRoute($menu_id) {
		$cus_sql="SELECT * FROM " . DB_PREFIX . "menu_cus WHERE menu_route_id=".$menu_id;
		$query = $this->db->query($cus_sql);
		return $query->rows;
	}
	
	public function getTotalMenus() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "menu");
		
		return $query->row['total'];
	}	
}
?>